package service;

import dao.SaleDao;
import model.Computer;
import model.Customer;
import model.SaleRecord;
import java.util.List;
import java.util.Scanner;
import java.util.UUID;

public class SaleService {
    private SaleDao saleDao;
    private ComputerService computerService;
    private CustomerService customerService;
    private Scanner scanner;

    public SaleService(ComputerService computerService, CustomerService customerService) {
        this.saleDao = new SaleDao();
        this.computerService = computerService;
        this.customerService = customerService;
        this.scanner = new Scanner(System.in);
    }

    public void makeSale() {
        System.out.println("\n=== 进行销售 ===");

        computerService.viewAllComputers();
        System.out.print("请输入要销售的电脑ID: ");
        String computerId = scanner.nextLine();

        Computer computer = computerService.findComputerById(computerId);
        if (computer == null) {
            System.out.println("❌ 电脑不存在！");
            return;
        }

        customerService.viewAllCustomers();
        System.out.print("请输入客户ID: ");
        String customerId = scanner.nextLine();

        Customer customer = customerService.findCustomerById(customerId);
        if (customer == null) {
            System.out.println("❌ 客户不存在！");
            return;
        }

        System.out.print("请输入购买数量: ");
        int quantity = scanner.nextInt();
        scanner.nextLine();

        if (computer.getStock() < quantity) {
            System.out.println("❌ 库存不足！当前库存: " + computer.getStock());
            return;
        }

        double totalAmount = computer.getPrice() * quantity;

        System.out.printf("确认销售：%s 给 %s，数量：%d，总金额：¥%.2f\n",
                computer.getModel(), customer.getName(), quantity, totalAmount);
        System.out.print("确认销售？(y/n): ");
        String confirm = scanner.nextLine();

        if (!confirm.equalsIgnoreCase("y")) {
            System.out.println("销售已取消");
            return;
        }

        String saleId = "S" + UUID.randomUUID().toString().substring(0, 7);

        SaleRecord saleRecord = new SaleRecord(saleId, computerId, customerId, quantity, totalAmount);

        computer.setStock(computer.getStock() - quantity);
        computerService.updateComputer(computer);

        saleDao.addSale(saleRecord);

        System.out.println("销售成功！");
        System.out.println("销售单号: " + saleId);
        System.out.println("总金额: ¥" + totalAmount);
    }

    public void viewAllSales() {
        System.out.println("\n=== 所有销售记录 ===");
        List<SaleRecord> sales = saleDao.getAllSales();

        if (sales.isEmpty()) {
            System.out.println("暂无销售记录！");
            return;
        }

        System.out.println("销售ID\t电脑ID\t客户ID\t数量\t总金额\t销售时间");
        System.out.println("----------------------------------------------------------------");
        for (SaleRecord sale : sales) {
            System.out.printf("%s\t%s\t%s\t%d\t¥%.2f\t%s\n",
                    sale.getId(), sale.getComputerId(), sale.getCustomerId(),
                    sale.getQuantity(), sale.getTotalAmount(), sale.getSaleTime());
        }
    }

    public void viewSalesReport() {
        System.out.println("\n=== 销售统计报告 ===");
        List<SaleRecord> sales = saleDao.getAllSales();

        if (sales.isEmpty()) {
            System.out.println("暂无销售记录！");
            return;
        }

        double totalRevenue = 0;
        int totalComputersSold = 0;

        for (SaleRecord sale : sales) {
            totalRevenue += sale.getTotalAmount();
            totalComputersSold += sale.getQuantity();
        }

        System.out.println("总销售额: ¥" + totalRevenue);
        System.out.println("总销售电脑数量: " + totalComputersSold);
        System.out.println("总交易次数: " + sales.size());
        System.out.printf("平均每单金额: ¥%.2f\n", totalRevenue / sales.size());
    }
}